package com.ruoyi.web.controller.medical.registration;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.MatchException;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.users.domain.Patient;
import com.ruoyi.users.service.IDeptService;
import com.ruoyi.users.service.IPatientService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.medical.domain.Registration;
import com.ruoyi.medical.service.IRegistrationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 挂号信息管理Controller
 *
 * @author ruoyi
 * @date 2024-02-26
 */
@RestController
@RequestMapping("/medical/registration")
public class RegistrationController extends BaseController {
    @Autowired
    private IRegistrationService registrationService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询科室列表
     */
    @PostMapping(value = "/listDeptName")
    public AjaxResult listDeptName() {
//        List<Dept> deptList = deptService.selectDeptList(null);
        List<SysDept> hospitalDeptList = sysDeptService.selectChildrenDeptById(110L);//医院的id是110
        List<SysDept> delList = new ArrayList<>();//中转集合
        for (SysDept sysDept : hospitalDeptList) {
            //被删除了,需要从列表中删除
            if (sysDept.getDelFlag().equals("2")) {
                delList.add(sysDept);
            }
        }
        hospitalDeptList.removeAll(delList);
        return AjaxResult.success(hospitalDeptList);
    }


    /**
     * 查询挂号信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('medical:registration:list')")
    @GetMapping("/list")
    public TableDataInfo list(Registration registration) {
        startPage();
        List<Registration> list = registrationService.selectRegistrationList(registration);
        return getDataTable(list);
    }

    /**
     * 导出挂号信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('medical:registration:export')")
    @Log(title = "挂号信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Registration registration) {
        List<Registration> list = registrationService.selectRegistrationList(registration);
        ExcelUtil<Registration> util = new ExcelUtil<Registration>(Registration.class);
        util.exportExcel(response, list, "挂号信息管理数据");
    }

    /**
     * 获取挂号信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('medical:registration:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Registration registration = registrationService.selectRegistrationById(id);
        return success(registration);
    }

    /**
     * 新增挂号信息管理
     */
    @PreAuthorize("@ss.hasPermi('medical:registration:add')")
    @Log(title = "挂号信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Registration registration) throws Exception {
        SysUser patient = userService.checkPatientAndDoctor(registration.getPatientName(),registration.getIdNumber(),null, null);
        if (patient == null) {
            return AjaxResult.error("患者姓名或身份证号输入错误");
        }
        if (!"11".equals(patient.getUserType())) {
            return AjaxResult.error("该用户不属于患者用户");
        }
        System.err.println(registration);
        return toAjax(registrationService.insertRegistration(registration));
    }

    /**
     * 修改挂号信息管理
     */
    @PreAuthorize("@ss.hasPermi('medical:registration:edit')")
    @Log(title = "挂号信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Registration registration) {
        SysUser patient = userService.checkPatientAndDoctor(registration.getPatientName(),registration.getIdNumber(),null, null);
        if (patient == null) {
            return AjaxResult.error("患者姓名或身份证号输入错误");
        }
        if (!"11".equals(patient.getUserType())) {
            return AjaxResult.error("该用户不属于患者用户");
        }
        return toAjax(registrationService.updateRegistration(registration));
    }

    /**
     * 删除挂号信息管理
     */
    @PreAuthorize("@ss.hasPermi('medical:registration:remove')")
    @Log(title = "挂号信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(registrationService.deleteRegistrationByIds(ids));
    }
}
